package io.renren.modules.weixin.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.weixin.entity.PingjiaEntity;
import io.renren.modules.weixin.service.PingjiaService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.DateUtils;


/**
 * 评价
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-02-02 09:01:34
 */
@RestController
@RequestMapping("weixin/pingjia")
@Api("微信小程序-评价接口")
public class PingjiaController {
    @Autowired
    private PingjiaService pingjiaService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = pingjiaService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("信息")
    public R info(@PathVariable("id") Integer id) {
        PingjiaEntity pingjia = pingjiaService.selectById(id);

        return R.ok().put("pingjia", pingjia);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("保存")
    public R save(@RequestBody PingjiaEntity pingjia) {
        pingjia.setDate(DateUtils.format(new Date()));
        pingjiaService.insert(pingjia);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改")
    public R update(@RequestBody PingjiaEntity pingjia) {
        pingjiaService.updateById(pingjia);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除")
    public R delete(@RequestBody Integer[] ids) {
        pingjiaService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 列表
     */
    @GetMapping("/listbycaipuid")
    @ApiOperation("列表")
    public R listbycaipuid(@RequestParam Map<String, Object> params) {
        PageUtils page = pingjiaService.queryByCaipuid(params);
        return R.ok().put("page", page);
    }

    @GetMapping("/zan/{id}")
    @ApiOperation("点赞")
    public R zan(@PathVariable("id") Integer id) {
        try {
            PingjiaEntity pingjia = pingjiaService.selectById(id);
            pingjia.setZan(pingjia.getZan() + 1);
            if (!pingjiaService.updateById(pingjia))
                return R.error();
            else
                return R.ok();
        } catch (Exception e) {
            return R.error();
        }
    }

}
